package com.aruba.controller;

import com.aruba.bean.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.ServletRequest;
import java.util.Date;

@Controller
public class HelloController {

    @RequestMapping("hello.do")
    public String helloForward() {
        System.out.println("helloForward");
        return "hello.jsp";
    }

    @RequestMapping(value = "hello2.do", method = RequestMethod.GET, params = {"name!=", "pwd"}, headers = {"Accept-Encoding=gzip, deflate"})
    public String hello2Forward() {
        System.out.println("hello2Forward");
        return "hello2";
    }


    @RequestMapping(value = "hello2", method = RequestMethod.GET)
    public String hello2() {
        return "hello2";
    }

    @RequestMapping("requestParam1")
    @ResponseBody
    public String requestParam1(String username) {
        System.out.println(username);
        return "success";
    }

    @RequestMapping("requestParam2")
    @ResponseBody
    public String requestParam2(String username, @RequestParam("pwd") String password) {
        System.out.println("username：" + username + "password:" + password);
        return "success";
    }

    @RequestMapping("requestParam3")
    @ResponseBody
    public String requestParam3(User user) {
        System.out.println(user.toString());
        return "success";
    }

    @RequestMapping("requestParam4/{id}/{name}")
    @ResponseBody
    public String requestParam4(@PathVariable("id") Integer id, @PathVariable("name") String name) {
        System.out.println("id:" + id.toString() + "name:" + name);
        return "success";
    }

    @RequestMapping(value = "helloForward")
    public String forward() {
        return "forward:/hello2";
    }

    @RequestMapping(value = "helloForwardView")
    public View forwardView() {
        View ret = new InternalResourceView("hello2");
        return ret;
    }

    @RequestMapping(value = "helloForwardModelView")
    public ModelAndView forwardModelView() {
        ModelAndView ret = new ModelAndView();
//        ret.setView(new InternalResourceView("hello2"));
        ret.setViewName("hello2");

        return ret;
    }

    @RequestMapping(value = "helloRedirect")
    public String redirect() {
        return "redirect:/hello2";
    }

    @RequestMapping(value = "helloRedirectView")
    public View redirectView() {
        View ret = new RedirectView("hello2");
        return ret;
    }

    @RequestMapping(value = "helloRedirectModelView")
    public ModelAndView redirectModelView() {
        ModelAndView ret = new ModelAndView();
//        ret.setView(new RedirectView("hello2"));
        ret.setViewName("redirect:/hello2");

        return ret;
    }

    @RequestMapping(value = "respJson")
    @ResponseBody
    public User respJson(String name, Integer age) throws JsonProcessingException {
        System.out.println("name:" + name + " age:" + age);
        return new User("张三", 0, 18, new Date());
    }
}
